package com.tbit.uqbike.object.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.tbit.uqbike.manager.exception.BaseException;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * @author yee_
 * @version 1.0
 * @date 2023/2/19 22:00
 */
@Getter
@Setter
public class PowerChangeMonitorConfig {

    /**
     * 区域id
     */
    @TableId(type = IdType.INPUT)
    @NotNull(message = "请填写区域id")
    private Integer accountId;
    /**
     * 馈电最大值
     */
    @NotNull(message = "请填写馈电截止电量")
    @Min(value = -1, message = "换电区间设置 馈电最大值为不能小于-1")
    private Integer powerFeedMax;
    /**
     * 低电最小值
     */
    @NotNull(message = "请填写低电起始电量")
    private Integer powerLowMin;
    /**
     * 低电最大值
     */
    @NotNull(message = "请填写低电截止电量")
    private Integer powerLowMax;
    /**
     * 中电最小值
     */
    @NotNull(message = "请填写中电起始电量")
    private Integer powerMiddleMin;
    /**
     * 中电最大值
     */
    @NotNull(message = "请填写中电截止电量")
    @Max(value = 99, message = "中电最高值为99")
    private Integer powerMiddleMax;
    /**
     * 高电最小值
     */
    @NotNull(message = "请填写高电起始电量")
    private Integer powerHighMin;

    /**
     * 闲置区间 短时最小值
     */
    @NotNull(message = "请填写闲置区间 短时起始时长")
    @Min(value = 1, message = "闲置区间短时最小不能低于1")
    private Integer idleShortMin;
    /**
     * 闲置区间 短时最大值
     */
    @NotNull(message = "请填写闲置区间 短时截止时长")
    private Integer idleShortMax;
    /**
     * 闲置区间 中时最小值
     */
    @NotNull(message = "请填写闲置区间 中时起始时长")
    private Integer idleMiddleMin;
    /**
     * 闲置区间 中时最大值
     */
    @NotNull(message = "请填写闲置区间 中时截止时长")
    private Integer idleMiddleMax;
    /**
     * 闲置区间 长时最大值
     */
    @NotNull(message = "请填写闲置区间 长时起始时长")
    private Integer idleLongMin;


    @NotNull(message = "请填写无订单区间 长时起始时长")
    private Integer noOrderLong;

    @NotNull(message = "请填写无订单区间 中时起始时长")
    private Integer noOrderMiddleMin;

    @NotNull(message = "请填写无订单区间 中时截止时长")
    private Integer noOrderMiddleMax;

    @NotNull(message = "请填写无订单区间 短时起始时长")
    private Integer noOrderShortMin;

    @NotNull(message = "请填写无订单区间 短时截止时长")
    private Integer noOrderShortMax;

    @NotNull(message = "请填写长时区间 长时起始时长")
    private Integer longOrder;

    @NotNull(message = "请填写长时区间 中时起始时长")
    private Integer longOrderMiddleMin;

    @NotNull(message = "请填写长时区间 中时截止时长")
    private Integer longOrderMiddleMax;

    @NotNull(message = "请填写长时区间 短时起始时长")
    private Integer longOrderShortMin;

    @NotNull(message = "请填写长时区间 短时起始时长")
    private Integer longOrderShortMax;



    public void intervalValueValid() {
        if ((this.powerFeedMax + 1) != this.powerLowMin) {
            throw new BaseException("低电起始电量不为馈电最大起始电量的+1值,请重新配置");
        }
        if (this.powerLowMin >= this.powerLowMax) {
            throw new BaseException("低电起始电量不能大于或等于低电截止电量,请重新配置");
        }
        if ((this.powerLowMax + 1) != this.powerMiddleMin) {
            throw new BaseException("中电起始电量不为馈电最大起始电量的+1值,请重新配置");
        }
        if (this.powerMiddleMin >= this.powerMiddleMax) {
            throw new BaseException("中电起始电量不能大于或等于中电截止电量,请重新配置");
        }
        if ((this.powerMiddleMax + 1) != this.powerHighMin) {
            throw new BaseException("高电起始电量不为馈电最大起始电量的+1值,请重新配置");
        }
        if (this.idleShortMin >= this.idleShortMax) {
            throw new BaseException("低时起始时长不能大于或等于低时截止时长,请重新配置");
        }
        if ((this.idleShortMax + 1) != this.idleMiddleMin) {
            throw new BaseException("中时起始时长不为短时截止时长的+1值,请重新配置");
        }
        if (this.idleMiddleMin >= this.idleMiddleMax) {
            throw new BaseException("中时起始时长不能大于或等于中时截止时长,请重新配置");
        }
        if ((this.idleMiddleMax + 1) != this.idleLongMin) {
            throw new BaseException("长时起始时长不为中时截止时长的+1值,请重新配置");
        }
    }

    public PowerChangeMonitorConfig putDefaultValue(Integer accountId) {
        this.accountId = accountId;
        this.powerFeedMax = 0;
        this.powerLowMin = 1;
        this.powerLowMax = 20;
        this.powerMiddleMin = 21;
        this.powerMiddleMax = 40;
        this.powerHighMin = 41;
        this.idleShortMin = 6;
        this.idleShortMax = 24;
        this.idleMiddleMin = 25;
        this.idleMiddleMax = 72;
        this.idleLongMin = 73;
        this.noOrderShortMin = 24;
        this.noOrderShortMax = 49;
        this.noOrderMiddleMin = 49;
        this.noOrderMiddleMax = 73;
        this.noOrderLong = 73;
        this.longOrderShortMax = 7;
        this.longOrderShortMin = 1;
        this.longOrderMiddleMin = 7;
        this.longOrderMiddleMax = 13;
        this.longOrder = 13;
        return this;
    }

    /**
     * 校验无订单和超时长是否有默认值
     */
    public void checkNull(){
        this.noOrderShortMin = Objects.isNull(this.noOrderShortMin) ? 24 : this.noOrderShortMin;
        this.noOrderShortMax = Objects.isNull(this.noOrderShortMax) ? 49 : this.noOrderShortMax;
        this.noOrderMiddleMin = Objects.isNull(this.noOrderMiddleMin) ? 49 : this.noOrderMiddleMin;
        this.noOrderMiddleMax = Objects.isNull(this.noOrderMiddleMax) ? 73 : this.noOrderMiddleMax;
        this.noOrderLong = Objects.isNull(this.noOrderLong) ? 73 : this.noOrderLong;
        this.longOrderShortMax = Objects.isNull(this.longOrderShortMax) ? 7 : this.longOrderShortMax;
        this.longOrderShortMin = Objects.isNull(this.longOrderShortMin) ? 1 : this.longOrderShortMin;
        this.longOrderMiddleMin = Objects.isNull(this.longOrderMiddleMin) ? 7 : this.longOrderMiddleMin;
        this.longOrderMiddleMax = Objects.isNull(this.longOrderMiddleMax) ? 13 : this.longOrderMiddleMax;
        this.longOrder = Objects.isNull(this.longOrder) ? 13 : this.longOrder;
    }


    public String matchPowerRange(Integer oldSoc) {
        if (oldSoc == null) {
            return "电量值:未知";
        }
        if (oldSoc >= -1 && oldSoc <= this.powerFeedMax) {
            return "馈电（-1% ~ " + this.powerFeedMax + "%）";
        }
        if (oldSoc >= powerLowMin && oldSoc <= powerLowMax) {
            return "低电（" + this.powerLowMin + "% ~ " + this.powerLowMax + "%）";
        }
        if (oldSoc >= powerMiddleMin && oldSoc <= powerMiddleMax) {
            return "中电（" + this.powerMiddleMin + "% ~ " + this.powerMiddleMax + "%）";
        }
        if (oldSoc >= powerHighMin && oldSoc <= 100) {
            return "高电（" + this.powerHighMin + "% ~ 100%）";
        }
        return "电量值:" + oldSoc + "%";
    }

    public String matchDispatchRange(Integer idleTime) {
        if (idleTime == null) {
            return "闲置时长:未知";
        }
        if (idleTime >= this.idleShortMin && idleTime <= this.idleShortMax) {
            return "短时（" + this.idleShortMin + " ~ " + this.idleShortMax + "小时）";
        }
        if (idleTime >= this.idleMiddleMin && idleTime <= this.idleMiddleMax) {
            return "中时（" + this.idleMiddleMin + " ~ " + this.idleMiddleMax + "小时）";
        }
        if (idleTime >= this.idleLongMin) {
            return "长时（" + this.idleLongMin + " ~ ∞小时）";
        }
        return "闲置时长:" + idleTime + "小时";
    }

}
